export default {
    data() {
        return {
            tableData: [],
            page: 1,
            limit: 10,
            count: 0,
        }
    },
    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        that.getList();
    },

    //方法
    methods: {
        /**
         * 列表
         */
        getList() {
            var that = this;
            that.tableData = [];
            //请求的数据
            var formData = {};
            formData.token = that.token;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Bannerimg/index", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        for (var i in res.body.list) {
                            if (res.body.list[i].status === 1) {
                                var status_text = '展示';
                            } else {
                                var status_text = '不展示';
                            }
                            that.tableData.push({
                                i: i,
                                id: res.body.list[i].id,
                                img: "<img src =" + res.body.list[i].picture + " style='height:100px'/ >",
                                sort: res.body.list[i].sort,
                                url: res.body.list[i].url,
                                status: status_text,
                            });
                        }
                        that.count = res.body.count;
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 添加广告
         */
        add: function () {
            this.$router.push('/sowing/add_swiper');
        },

        /**
         * 修改
         * @param id
         */
        edit: function (id) {
            var that = this;
            that.$router.push({path: '/sowing/add_swiper', query: {swiper_id: id}});
        },

        /**
         * 删除
         * @param e
         */
        deleteRow: function (e) {
            var that = this;
            that.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.doDelete(e);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //执行删除
        doDelete: function (e) {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/Bannerimg/del", {
                token: that.token,
                id: e
            }, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '删除成功' }`
                    });
                    that.getList();
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 下一页
         * @param currentPage
         */
        handleCurrentChange: function (currentPage) {
            var that = this;
            that.page = currentPage;
        },
    }
}

